package com.sunfeng.two.p1;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @Author : 清风冷影
 * @Description:
 * @Date : 2021/8/20  13:54
 */
public class Worker01 {

    private static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        final Channel channel = ConnectionUtil.getChannel();
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            try {
                TimeUnit.MILLISECONDS.sleep(10);
                System.out.println(" 接受到消息: " + new String(message.getBody()));

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        };
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println(consumerTag+"取消接受逻辑");
        };

        //autoAck设置为false 可以防止消息不必要的丢失   建议设置为false
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);

    }
}
